using Microsoft.AspNetCore.Mvc;
using MyHomeworkWebAPi.Models.DTOs;
using MyHomeworkWebAPi.Models.response;
using MyHomeworkWebAPi.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MyHomeworkWebAPi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class LogController : ControllerBase
    {
        private readonly LogService _logService;

        public LogController(LogService logService)
        {
            _logService = logService;
        }

        [HttpGet]
        public async Task<IActionResult> GetAll()
        {
            try
            {
                var logs = await _logService.GetAllAsync();
                return Ok(ApiResponse<List<LogDTO>>.SuccessResponse(logs));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<List<LogDTO>>.ErrorResponse(ex.Message, 400));
            }
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetById(Guid id)
        {
            try
            {
                var log = await _logService.GetByIdAsync(id);
                if (log == null)
                {
                    return NotFound(ApiResponse<LogDTO>.ErrorResponse("日志不存在", 404));
                }
                return Ok(ApiResponse<LogDTO>.SuccessResponse(log));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<LogDTO>.ErrorResponse(ex.Message, 400));
            }
        }

        [HttpPost]
        public async Task<IActionResult> Create(LogDTO logDTO)
        {
            try
            {
                var log = await _logService.CreateAsync(logDTO);
                return CreatedAtAction(nameof(GetById), new { id = log.Id }, ApiResponse<LogDTO>.SuccessResponse(log, "创建成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<LogDTO>.ErrorResponse(ex.Message, 400));
            }
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> Update(Guid id, LogDTO logDTO)
        {
            try
            {
                var log = await _logService.UpdateAsync(id, logDTO);
                if (log == null)
                {
                    return NotFound(ApiResponse<LogDTO>.ErrorResponse("日志不存在", 404));
                }
                return Ok(ApiResponse<LogDTO>.SuccessResponse(log, "更新成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<LogDTO>.ErrorResponse(ex.Message, 400));
            }
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            try
            {
                var result = await _logService.DeleteAsync(id);
                if (!result)
                {
                    return NotFound(ApiResponse<bool>.ErrorResponse("日志不存在", 404));
                }
                return Ok(ApiResponse<bool>.SuccessResponse(true, "删除成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<bool>.ErrorResponse(ex.Message, 400));
            }
        }
    }
} 